package com.hww.v2_mtfsdk.provider;

import com.hww.common.MyAppThis;
import com.hww.common.util.SharePreferencesUtils;
import com.hww.common.util.StringUtils;
import com.hww.common.util.ThreadUtils;
import com.hww.v2_mtfsdk.Constant;

import net.facelib.akcore.AuthParameter;
import net.facelib.akcore.BaseLicenseManager;
import net.facelib.akcore.LicenseManager;
import net.facelib.akcore.ProductInitStatus;
import net.facelib.authkernel.ServiceSecurityException;
import net.facelib.live.FaceLiveAndroid;
import net.facelib.mtfsdk.FaceApiMtfV2Android;

/**
 * @author hww
 * @date 0011 2024/7/11
 * email: heww@zhongyuntech.cn
 * description:
 */
public class SDKV2LicenseManager {

    private SDKV2LicenseManager() {
    }

    /**
     * 获取授权
     *
     * @param callBack   回调
     * @param isFaceLive 是否开启活体检测，true：开启活体检测，false：未开启活体检测
     */
    public static void onlineLicense(final LicenseStatusCallBack callBack, boolean isFaceLive) {
        ThreadUtils.getCpuPool().execute(() -> {
            try {

                LicenseManager licenseManager = FaceApiMtfV2Android.getInstance().getLicenseManager();
                /*
                 * 此配置根据个人需要进行调用
                 * @param connTimeout 连接超时参数(毫秒),小于等于0使用默认值(4秒)
                 * @param sendTimeout 发送超时参数(毫秒),小于等于0使用默认值(4秒)
                 * @param recvTimeout 接收超时参数(毫秒),小于等于0使用默认值(4秒)
                 * @param debug 是否输出调试信息
                 */
                BaseLicenseManager.setInitParameter(30000, 0, 0, true);

                /*指定授权参数*/
                ProviderMtfV2Custom providerCustom = new ProviderMtfV2Custom();
                String authorID = providerCustom.getAuthorID();
                String productSign = providerCustom.getProductSign();
                if (StringUtils.isEmpty(authorID) || StringUtils.isEmpty(productSign)) {
                    if (null != callBack) {
                        callBack.failed("\n组织代码或产品签名为空，未配置 v2.txt 配置文件\n/sdcard/v2.txt  /Android/data/包名/files/v2.txt");
                    }
                    return;
                }

                licenseManager.withProvider(providerCustom);
                if (ProductInitStatus.PRODUCT_INIT_OK == licenseManager.validateLicense()) {
                    String msg = "local init success   path= " + providerCustom.get().get(AuthParameter.LICENSE_FILE);
                    if (isFaceLive) {
                        onlineLicenseLive(callBack, msg);
                    } else {
                        if (null != callBack) {
                            callBack.success(msg);
                        }
                    }
                } else {
                    /*产品签名授权[AUPS]*/
                    licenseManager.aups();
                    if (null != callBack) {
                        ProductInitStatus productInitStatus = licenseManager.validateLicense();
                        if (ProductInitStatus.PRODUCT_INIT_OK == productInitStatus) {
                            SharePreferencesUtils.putString(MyAppThis.getThis(), Constant.SHARE_LICENSE_AUTHOR_ID, authorID);
                            SharePreferencesUtils.putString(MyAppThis.getThis(), Constant.SHARE_LICENSE_PRODUCT_SIGN, productSign);
                            String msg = "online init success   path= " + providerCustom.get().get(AuthParameter.LICENSE_FILE);
                            if (isFaceLive) {
                                onlineLicenseLive(callBack, msg);
                            } else {
                                callBack.success(msg);
                            }
                        } else {
                            String msg = "online init fail  path= " + providerCustom.get().get(AuthParameter.LICENSE_FILE);
                            callBack.failed(msg + "\ncode =" + productInitStatus.error + "   msg=" + productInitStatus.msg);
                        }
                    }
                }

            } catch (ServiceSecurityException e) {
                callBack.failed(String.format("code=%s  name=%s  msg=%s", e.getType().getValue(), e.getType().name(), e.getMessage()));
            } catch (Exception e) {
                callBack.failed(e.getMessage());
            }
        });
    }

    private static void onlineLicenseLive(final LicenseStatusCallBack callBack, String successMsg) {
        try {
            LicenseManager licenseManager = FaceLiveAndroid.getInstance().getLicenseManager();
            /*AkCore.setInitParameter(ip, 26441, 20000, 0, 0, null, true);*/
            ProviderLiveCustom providerCustom = new ProviderLiveCustom();
            licenseManager.withProvider(providerCustom);
            if (ProductInitStatus.PRODUCT_INIT_OK == licenseManager.validateLicense()) {
                if (null != callBack) {
                    callBack.success(successMsg + "\n(L)local init success   path= " + providerCustom.get().get(AuthParameter.LICENSE_FILE));
                }
            } else {
                /*产品签名授权[AUPS]*/
                licenseManager.aups();
                if (null != callBack) {
                    ProductInitStatus productInitStatus = licenseManager.validateLicense();
                    if (ProductInitStatus.PRODUCT_INIT_OK == productInitStatus) {
                        callBack.success(successMsg + "\nonline init success    path= " + providerCustom.get().get(AuthParameter.LICENSE_FILE));
                    } else {
                        callBack.failed(successMsg + "\nonline init fail   path= " + providerCustom.get().get(AuthParameter.LICENSE_FILE) + "\ncode =" + productInitStatus.error + "   msg=" + productInitStatus.msg);
                    }
                }
            }
        } catch (ServiceSecurityException e) {
            callBack.failed(String.format("code=%s  name=%s  msg=%s", e.getType().getValue(), e.getType().name(), e.getMessage()));
        } catch (Exception e) {
            callBack.failed(e.getMessage());
        }
    }

}
